package com.wanqing.community.dao.impl;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.wanqing.community.bean.DepartmentBean;
import com.wanqing.community.dao.DepartmentDao;
import com.wanqing.community.dao.util.MyBatisDao;
import com.wanqing.community.util.ConditionParse;
import com.wanqing.community.util.NumberDealTools;
import com.wanqing.community.util.StringTools;

/**
 * 部门管理DAO接口的实现类
 */
@Repository("departmentDao")
public class DepartmentDaoImpl extends MyBatisDao<DepartmentBean, String> implements DepartmentDao{
	
	private final String NAMES_SPACE = "com.wanqing.community.bean.DepartmentBean";
	
	/**
	 * <p>Description:添加部门<p>
	 * <p>modifiContent:<p>
	 * @param department
	 * @return
	 */
	public int addDepartment(DepartmentBean department) {		
		return this.save(NAMES_SPACE+".addDepartment", department);
	}
	
	/**
	 * <p>Description:删除部门<p>
	 * <p>modifiContent:<p>
	 * @param list
	 * @return
	 */
	public int deleteDepartment(List<String> list) {		
		return this.deleteBatchById(NAMES_SPACE+".deleteDepartmentBatch", list);
	}
	
	/**
	 * <p>Description:根据条件得到部门的数量<p>
	 * <p>modifiContent:</p>
	 * @param condParse
	 * @return
	 */
	public int getDepartmentCount(ConditionParse condParse) {		
		return this.getCount(NAMES_SPACE+".getDepartmentCount", condParse);
	}
	
	/**
	 * <p>Description:根据查询条件查询部门信息<p>
	 * <p>modifiContent:</p>
	 * @param condParse
	 * @return
	 */
	public List<DepartmentBean> queryDepartment(ConditionParse parse) {		
		return this.queryForList(NAMES_SPACE+".queryDepartment", parse);
	}
	
	/**
	 * <p>Description:修改部门<p>
	 * <p>modifiContent:<p>
	 * @param department
	 * @return
	 */
	public int updateDepartment(DepartmentBean department) {		
		return this.update(NAMES_SPACE+".updateDepartment", department);
	}
	
	/**
	 * <p>Description:修改部门的是否为根<p>
	 * <p>modifiContent:<p>
	 * @param dleaf
	 * @param id
	 * @return
	 */
	public int updateDepartmentLeaf(boolean dleaf, String id) {
		DepartmentBean db = new DepartmentBean();
		db.setId(id);
		db.setDleaf(dleaf);
		return this.update(NAMES_SPACE+".updateDepartmentLeaf", db);
	}

	/**
	 * <p>Description:采用递归删除部门以及其下属部门<p>
	 * <p>modifiContent:<p>
	 * @param orgId
	 * @return
	 */
	public int deleteDepartment(String orgId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgId", orgId);
		map.put("r_message", Types.VARCHAR);
		this.delete(NAMES_SPACE+".deleteDepartment", map);
		String result = StringTools.objectToString(map.get("r_message"));
		return NumberDealTools.stringTranceInt(result);
	}

	/**
	 * <p>Description:通过Id判断<p>
	 * <p>modifiContent:</p>
	 * @param  department
	 * @return 
	 */
	public DepartmentBean findDepartmentById(String dId) {		
		return this.findObjectById(NAMES_SPACE+".findDepartmentById", dId);
	}
	
}
